package com.IUT.biblio;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.GregorianCalendar;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.IUT.biblio.model.*;

public class ctrlExemplaire extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	public void doGet(	HttpServletRequest request, 
			HttpServletResponse response)
			throws IOException, ServletException{
		
		if(getServletConfig().getInitParameter("action").toString().equals("add")){
			request.getRequestDispatcher("../ajoutEx.jsp").forward(request, response);
		}
		else if(getServletConfig().getInitParameter("action").toString().equals("consult")){
			request.getRequestDispatcher("../consultOuv.jsp").forward(request, response);
		}
		else{
			request.setAttribute("error", "Action interdite : " + getServletConfig().getInitParameter("action").toString());
			request.getRequestDispatcher("../error.jsp").forward(request, response);
		}
		
	}
	
	public void doPost(     HttpServletRequest request, 
            HttpServletResponse response)
            throws IOException, ServletException{

		ServletContext context = getServletContext();
		
		if(request.getParameter("actForm")==null){
			request.setAttribute("error", "Erreur inconnue");
			request.getRequestDispatcher("../error.jsp").forward(request, response);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////
		else if(request.getParameter("actForm").toString().equals("ajoutEx")){
			
			if(request.getParameter("dateJ").isEmpty() 
			|| request.getParameter("dateM").isEmpty()
			|| request.getParameter("dateA").isEmpty()){
						
						request.setAttribute("error", "Tout les champs sont obligatoires !!!");
						request.getRequestDispatcher("../error.jsp").forward(request, response);
					}
					else{
						String ISBN = request.getParameter("isbn").toString();
						GregorianCalendar dateReception = new GregorianCalendar(Integer.parseInt(request.getParameter("dateA").toString()),Integer.parseInt(request.getParameter("dateM").toString())-1,Integer.parseInt(request.getParameter("dateJ").toString()));
									
						Bibliotheque biblio =(Bibliotheque)context.getAttribute("biblio");
						Ouvrage o = biblio.unOuvrage(ISBN);
						String ID = o.numExSuivant().toString();
						Exemplaire e = new Exemplaire(ID,dateReception,o);
						o.lierExemplaire(e);
						
						request.setAttribute("texte", "Exemplaire ajouté, ID : " + ID);
						request.getRequestDispatcher("../debut.jsp").forward(request, response);
						
					}
		}
		//////////////////////////////////////////////////////////////////////////////////////////
		else{
			request.setAttribute("error", "Erreur inconnue2");
			request.getRequestDispatcher("../error.jsp").forward(request, response);
		}
}

}
